﻿using MiniFox.Infrastructure;
using System;

namespace MiniFox.Data
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class PagedResultSet<TEntity> : ResultSet<TEntity>, IPagedResultSet
    {
        int _pageIndex;
        int _pageSize;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        public PagedResultSet(int pageIndex, int pageSize) : this(pageIndex, pageSize, false)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="lazyLoad"></param>
        public PagedResultSet(int pageIndex, int pageSize, bool lazyLoad) : base(lazyLoad)
        {
            _pageIndex = pageIndex;
            _pageSize = pageSize;
        }
        /// <summary>
        /// 
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int StartRowIndex
        {
            get { return this.PageIndex * this.PageSize + 1; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int EndRowIndex
        {
            get { return (this.PageIndex + 1) * this.PageSize; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int PageCount
        {
            get { return (int)Math.Ceiling((double)RowCount / (double)PageSize); }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        protected override ResultObject<TResult> CreateResultObject<TResult>()
        {
            return new PagedResultObject<TResult>(this.RowCount, this.PageIndex, this.PageSize);
        }
    }
}
